
package examples;


import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;

public class FileToJList extends JFrame {

public FileToJList() {
 super("File Chooser Test Frame");
 setSize(350, 200);
 setDefaultCloseOperation(EXIT_ON_CLOSE);

 Container c = getContentPane();
 c.setLayout(new FlowLayout());

 JButton openButton = new JButton("Open");

 final Vector data = new Vector();
 final JList list = new JList(data);


 // Create a file chooser that opens up as an Open dialog
 openButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent ae) {

     JFileChooser chooser = new JFileChooser();
     chooser.setMultiSelectionEnabled(true);
     int option = chooser.showOpenDialog(FileToJList.this);

     if (option == JFileChooser.APPROVE_OPTION)
     {
        String location = chooser.getSelectedFile().getPath();

        String name = chooser.getSelectedFile().getName();

        System.out.println(location+"\n");

         String s = "";
         try
         {
            FileReader fr = new FileReader(location);
            BufferedReader br = new BufferedReader(fr);

            String line;
            while ((line = br.readLine()) != null)
            {
              data.add(line);
              System.out.print(line+"\n");
            }
            list.updateUI();
         
        }
         catch(IOException f)
        {

        }
       
     }
     else {
     }
   }
 }
 );


 
 c.add(openButton);
c.add(list);

}

public static void main(String args[]) {
 FileToJList sfc = new FileToJList();
 sfc.setVisible(true);
}
}
